package src.main.danli;

import java.util.HashMap;
import java.util.Map;

/**
 * 登记式单例模式
 */
public class RegistrationSingle {
    private RegistrationSingle(){};
    public static Map<String,Object> map= new HashMap<>();
    static {
        RegistrationSingle registrationSingle=new RegistrationSingle();
        map.put(registrationSingle.getClass().getName(),registrationSingle);
    }
    public static Object getRegistrationSingle(String name){
        if(name==null){
            name="demo.single.RegistrationSingle";
        }
        if(map.get(name)==null) {
            try {
                map.put(name, Class.forName(name).newInstance());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return map.get(name);
    }
}
